
package com.android.replace;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

public class MergeString {

    public static String getId(String value) {
        if (value == null) {
            return null;
        }
        if (value.length() == 0) {
            System.out.println();
        }
        if (value.contains("<string name=\"")) {
            value = value.replaceFirst("<string name=\"", "");
            value = value.trim();
        } else {
            return null;
        }
        int indexOf = value.indexOf("\"");
        if (indexOf <= 0) {
            return null;
        }
        value = value.substring(0, indexOf);
        return value;
    }

    public static void parseFile(String file) {
        if (TextUtils.isEmpty(file)) {
            return;
        }
        File destFile = new File(file + ".tmp");
        File sourceFile = new File(file);
        BufferedReader br = null;
        BufferedWriter bw = null;
        boolean success = false;
        HashMap<String, String> stringMap = new HashMap<String, String>();
        try {
            bw = new BufferedWriter(new FileWriter(destFile));
            br = new BufferedReader(new FileReader(sourceFile));
            String line;
            while ((line = br.readLine()) != null) {
                String copy = new String(line);
                String id = getId(line);
                if (copy.contains("crypt_keeper_desc")) {
                    System.out.println("hahah");
                }
                if (TextUtils.isEmpty(id)) {
                    continue;
                }
                if (!ids.containsKey(id)) {
                    bw.write(copy + "\r");
                    if (copy.contains("</string>")) {
                        System.out.println(copy);
                        continue;
                    }
                    while ((line = br.readLine()) != null) {
                        System.out.println(line);
                        bw.write(line + "\r");
                        if (line.contains("</string>")) {
                            break;
                        }
                    }
                    continue;
                }
            }
            success = true;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success = false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success = false;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    public static void getIds(String file) {
        if (TextUtils.isEmpty(file)) {
            return;
        }
        File sourceFile = new File(file);
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(sourceFile));
            String line;
            while ((line = br.readLine()) != null) {
                String id = getId(line);
                if (TextUtils.isEmpty(id) || ids.containsKey(id)) {
                    continue;
                }
                ids.put(id, id);
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    public static void listFile(String dir) {
        File root = new File(dir);
        File[] listFiles = root.listFiles();
        if (listFiles != null) {
            for (File file : listFiles) {
                if (file.isFile()) {
                    if (file.getPath().contains("string")) {
                        parseFile(file.getAbsolutePath());
                    }
                } else {
                    if (file.getPath().contains("values")) {
                        listFile(file.getAbsolutePath());
                    }
                }
            }
        }
    }

    private static HashMap<String, String> ids = new HashMap<String, String>();

    public static void main(String[] args) {
        String source = "D:\\Development\\Android\\CP960Git\\androidphone\\YLSetting\\res\\values\\strings.xml";
        getIds(source);
        String destination = "F:\\gittest\\Replace\\src\\com\\android\\replace\\xml\\strings_input.xml";
        parseFile(destination);
        //        listFile(source);
    }
}
